# -*- coding: utf-8 -*-
# @Time         : 2021/10/17 17:23
# @Author       : Jinxing Lin
# @StudentNumber: 20216523
# @Affiliation  : SUN YAT-SEN UNIVERSITY  SCHOOL OF SYSTEMS SCIENCE AND ENGINEERING
# @Mail         ：linjx83@mail2.sysu.edu.cn
# @FileName     : draw_line_graph.py
# @Software     : PyCharm

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def plot_line_chart(x_names, data, x_title, y_title, line_name, save_name=""):
    '''
        折线图
    :param x_names:横轴名称
    :param data:数据
    :param x_title:横轴标题
    :param y_title:纵轴标题
    :param line_name:折线名称
    :param save_name:图片保存名称
    :param yerr:方差
    :return:
    '''

    # plt.rc('font', family='Latin Modern Roman')
    plt.rc('font', family='Times New Roman')

    font = {"size": 36}
    f, ax = plt.subplots(figsize=(15, 15))  # 第一个是宽，第二个是高
    marks = ['8', 's', '*', '>', 'p', 'd', 'H', 'o', 'd']
    colors = ['pink',
              "#A4CC2E",
              '#3498DB',
              '#FEB308',
              '#9B59B6',
              "#E74C3C",
              'black',
              'cyan',
              'blue'
              ]

    colors = ["#8C564B", "#92D050", "#00B0F0", "#F59D56", "#BFBFBF",
              "#C00000", "#CC00FF", "#FF6699", "#FFD965"]

    linewidth = 3

    markersize = 15
    x = range(len(x_names))

    # 画虚线
    plt.axhline(y=0.002, ls="--", c="red", linewidth=linewidth)  # 添加水平直线

    for index in range(data.shape[0]):
        plt.plot(x, data[index][::-1],
                     markersize=markersize,
                     color=colors[index],
                     marker=marks[index],
                     label=line_name[index],
                     linewidth=linewidth)

    plt.xticks(x, x_names, fontsize=33, rotation=0)
    plt.yticks(fontsize=33)
    plt.xlabel(x_title, font, labelpad=15)
    plt.ylabel(y_title, font, labelpad=15)

    # 坐标轴设置
    ax.spines['top'].set_visible(True)
    ax.spines['right'].set_visible(True)
    ax.spines['bottom'].set_linewidth(linewidth)  # 设置底部坐标轴的粗细
    ax.spines['left'].set_linewidth(linewidth)
    ax.spines['top'].set_linewidth(linewidth)
    ax.spines['right'].set_linewidth(linewidth)

    # plt.tick_params(labelsize=28)

    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width, box.height * 1.0])
    ax.legend(loc='center left',
              bbox_to_anchor=(0.05, 1.08),
              ncol=3,
              fontsize=31)

    # 从0.1开始
    # ax.set_ylim(bottom=1)

    plt.tight_layout()
    plt.savefig('plot/{}.png'.format(save_name), dpi=1000, format='png', bbox_inches='tight')



# read from data802
name = r"result_cp_0.05_mean"
df = pd.read_csv("data/{}.csv".format(name), index_col=0, header=0)
x_names = list(df.columns)
line_name = list(df.index)
data = np.asarray(df)
x_title = r"$\alpha$" #"Proportion of train set"
y_title = "Mean square error of 5-fold cross validation on train set"
plot_line_chart(x_names, data, x_title, y_title, line_name=line_name, save_name=name)

